package com.llynosy.interview.base;

import com.github.pagehelper.PageInfo;
import com.llynosy.interview.po.Result;
import com.llynosy.interview.po.StatusCode;
import com.llynosy.interview.utils.CommUtils;
import com.llynosy.interview.utils.IDWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

//封装CRUD接口
public  class BaseController<E> {

    @Autowired
    protected AbstractCommCRUD<E> commCrudService;
    /**
     * 查询所有E
     * @return
     */
    @GetMapping
    public Result<List<E>> findAll(){
        List<E> list=commCrudService.findAll();
        return new Result<>(true, StatusCode.OK,"查询成功",list);
    }
    //根据ID查询
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public  Result<E> findById(@PathVariable("id")Long id){
        E e=commCrudService.findById(id);
        return new Result<>(true,StatusCode.OK,"查询成功",e);
    }

    /**
     * 根据ID更新元素E
     * @param id
     * @param e
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/{id}",method = RequestMethod.PUT)
    public Result updateE(@PathVariable("id")Long id, E e) throws  Exception{
        invokeMethodByName(e,id,"setId");
        invokeMethodByName(e, CommUtils.getDate(),"setCreateTime");
        commCrudService.update(e);
        return new Result<>(true, StatusCode.OK,"更新成功");
    }

    /**
     * 根据ID删除元素E
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable("id")Long id){
        commCrudService.delete(id);
        return new Result<>(true,StatusCode.OK,"删除成功");
    }

    @Autowired
    private IDWorker idWorker;

    /**
     * 增加元素E
     * @param
     * @return e
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result addE(@RequestBody E e) throws  Exception{
        invokeMethodByName(e, idWorker.nextId(),"setId");
        invokeMethodByName(e, CommUtils.getDate(),"setCreateTime");
        commCrudService.add(e);
        return new Result<>(true,StatusCode.OK,"增加成功");
    }

    //根据方法名字调用对象的方法
    private void invokeMethodByName(@RequestBody E e, Object param,String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Class eClass = e.getClass();
        Method[] methods = eClass.getDeclaredMethods();
        for(Method m:methods){
            if(m.getName().equals(methodName)){
                m.invoke(e,param);
            }
        }
    }

    /**
     * 条件查询
     * @param e
     * @return
     */
    @PostMapping("/search")
    public Result<List<E>> findList(@RequestBody E e){
        return new Result<>(true,StatusCode.OK,"查询成功",commCrudService.findList(e));
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable("page")Integer page, @PathVariable("size")Integer size){
        return new Result<>(true,StatusCode.OK,"查询成功",commCrudService.findPage(page,size));
    }

    /**
     * /条件分页查询
     * @param e
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) E e,@PathVariable("page")Integer page,@PathVariable("size")Integer size){
        return new Result<>(true,StatusCode.OK,"查询成功",commCrudService.findPage(e,page,size));
    }
}
